﻿/// <reference path="../libs/jquery-1.8.2-vsdoc.js" />
var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.OutlookConfigurations = SCA.UI.OutlookConfigurations || {};

SCA.UI.OutlookConfigurations = function () {

    var thisContext = this;
    this.SelectedFolderType  = null;

    //TODO: duplicated
    var FOLDER_TYPE_LIST = " Sobiens.Connectors.Entities.SharePoint.SPList";
    var FOLDER_TYPE_FOLDER = "Sobiens.Connectors.Entities.SharePoint.SPFolder";

    this.Setup = function () {
        GetButtonSelectLocation().click(function () { thisContext.OpenLocationTree() });
        this.Load();
    }

    this.Load = function () {

        if (SCA.AppConfiguration.OutlookConfigurations == null)
            return;

        var outlookConfiguration = SCA.AppConfiguration.OutlookConfigurations;

        if (outlookConfiguration.SaveAsWord) {
            GetRadioButtonWordDocument().attr("checked", true);
        }
        else {
            GetRadioButtonEmailItem().attr("checked", true);
        }

        GetCheckBoxUploadAutomatically().attr("checked", outlookConfiguration.UploadAutomatically);
        GetCheckBoxDoNotAskSaveLocation().attr("checked", outlookConfiguration.DontAskSaveAttachmentLocation);
        GetSpanFolderLocation().text(outlookConfiguration.DefaultAttachmentSaveFolderPath);

        FillDropDownList(SCA.AppConfiguration.SiteSettings, GetDropDownSiteSetting(), function (item) { return item.ID; }, function (item) { return item.Url; });
        GetDropDownSiteSetting().val(outlookConfiguration.DefaultAttachmentSaveFolderSiteSettingID);

        this.SelectedFolderType = outlookConfiguration.DefaultAttachmentSaveFolderType;
       

    }


    this.OpenLocationTree = function () {
        var treeParams = {
            allowSelectWeb: false,
            allowSelectList: true,
            allowSelectFolder: true,
            exploreDepth: "Folder"
        }
        var siteSettingUrl = GetDropDownSiteSettingSelectedText();

        this.LocationSelectionDialog = new SCA.UI.LocationSelection.Dialog("Location selection", siteSettingUrl, treeParams, SelectLocationCallback);
        this.LocationSelectionDialog.Open();
    }

    var SelectLocationCallback = function (result) {
        if (result != null) {

            GetSpanFolderLocation().text(result.absoluteUrl);

            this.SelectedFolderType = (result instanceof SCA.DAL.List)
                                        ? FOLDER_TYPE_LIST
                                        : (result instanceof SCA.DAL.Folder)
                                            ? FOLDER_TYPE_FOLDER
                                            : null;
        }
    }

    //TODO: duplicated
    var FillDropDownList = function (listOfItems, dropDownList, valueDelegate, textDelegate) {
        dropDownList.html("");

        if ((listOfItems != null) && (listOfItems.length > 0))
            for (var i = 0; i < listOfItems.length; i++) {
                var item = listOfItems[i];
                jQuery("<option value='" + valueDelegate(item) + "'>" + textDelegate(item) + "</option>").appendTo(dropDownList);
            }
    }


    var GetRadioButtonWordDocument = function () {
        return jQuery("#RadioButtonOutlookConfigurationsWordDocument");
    }

    var GetRadioButtonEmailItem = function () {
        return jQuery("#RadioButtonOutlookConfigurationsEmailItem");
    }


    var GetCheckBoxUploadAutomatically = function () {
        return jQuery("#CheckBoxOutlookConfigurationsUploadAutomaticallyOnDragAndDrop");
    }

    //var GetTextboxFolder = function () {
    //    return jQuery("#TextBoxOutlookLocation");
    //}

    var GetSpanFolderLocation = function()
    {
        return jQuery("#SpanOutlookLocation");
    }

    var GetCheckBoxDoNotAskSaveLocation = function () {
        return jQuery("#CheckBoxOutlookConfigurationsDoNotAskSaveLocation");
    }

    var GetDropDownSiteSetting = function () {
        return jQuery("#DropDownOutlookSiteSetting");
    }

    var GetDropDownSiteSettingSelectedText = function () {
        return jQuery("#DropDownOutlookSiteSetting option:selected").text()
    }

    var GetButtonSelectLocation = function () {
        return jQuery("#ButtonOutlookSelectLocation");
    }


    //TODO: add validation

    this.Save = function () {
        var outlookConfiguration = SCA.AppConfiguration.OutlookConfigurations;
        outlookConfiguration.SaveAsWord = GetRadioButtonWordDocument().is(":checked");
        outlookConfiguration.UploadAutomatically = GetCheckBoxUploadAutomatically().is(":checked");
        outlookConfiguration.DontAskSaveAttachmentLocation = GetCheckBoxDoNotAskSaveLocation().is(":checked");
        outlookConfiguration.DefaultAttachmentSaveFolderPath = GetSpanFolderLocation().text();
        outlookConfiguration.DefaultAttachmentSaveFolderSiteSettingID = GetDropDownSiteSetting().val();
        outlookConfiguration.DefaultAttachmentSaveFolderType = this.SelectedFolderType;
    }

    this.Setup();
};